package 第12周_AVL树和红黑树;

/**
 * 红黑树
 */
public class RBTree<K extends Comparable<K>, V> {

    private static final boolean RED = true;
    private static final boolean BLACK = false;

    private class Node{
        private K key;
        private V value;
        private Node left, right;
        public boolean color;

        public Node(K key, V value){
            this.key = key;
            this.value = value;
            left = null;
            right = null;
            color = RED;
        }
    }

    /** 根节点 */
    private Node root;
    private int size;

    public RBTree(){
        root = null;
        size = 0;
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    private boolean isRed(Node node){
        if (node == null){
            return BLACK;
        }
        return node.color;
    }


    /**
     * 左旋转
     * @param node
     * @return
     */
    private Node leftRotate(Node node){
        Node x = node.right;

        //左旋转
        node.right = x.left;
        x.left = node;

        x.color = node.color;
        node.color = RED;
        return x;
    }

    /**
     * 颜色翻转
     * @param node
     */
    private void flipColors(Node node){
        node.color = RED;
        node.left.color = BLACK;
        node.right.color = BLACK;
    }

    private Node rightRotate(Node node){
        Node x = node.left;

        //右旋转
        node.left = x.right;
        x.right = node;

        x.color = node.color;
        node.color = RED;

        return x;
    }

    /**
     * 向红黑树中添加元素
     * @param key
     * @param value
     */
    public void add(K key, V value){
        root = add(root, key, value);
        root.color = BLACK;
    }

    private Node add(Node node, K key, V value){
        if (node == null){
            size++;
            return new Node(key, value);  //默认插入红色节点
        }

        if (key.compareTo(node.key) < 0){
            node.left = add(node.left, key, value);
        }else if (key.compareTo(node.key) > 0){
            node.right = add(node.right, key, value);
        }else{
            node.value = value;
        }

        if (isRed(node.right) && !isRed(node.left)){
            node = leftRotate(node);
        }
        if (isRed(node.left) && isRed(node.left.left)){
            node = rightRotate(node);
        }
        if (isRed(node.left) && isRed(node.right)){
            flipColors(node);
        }

        return node;
    }

    /**
     * 根据键找到对应存放该键的节点
     * @param key
     * @return
     */
    public Node getNode(Node node, K key) {
        if (node == null) {
            return null;
        }

        if (key.compareTo(node.key) == 0) {
            return node;
        } else if (key.compareTo(node.key) < 0) {
            return getNode(node.left, key);
        } else {
           return getNode(node.right, key);
        }
    }

    /**
     * 判断该k是否存在
     * @param key
     * @return
     */
    public boolean contains(K key){
        return getNode(root, key) != null;
    }

    /**
     * 通过键获取值
     * @param key
     * @return
     */
    public V get(K key){
        Node node = getNode(root, key);
        return node == null ? null : node.value;
    }

    /**
     * 修改键对应的值
     * @param key
     * @param value
     */
    public void set(K key, V value){
        Node node = getNode(root, key);
        if (node == null){
            throw new IllegalArgumentException("该键不存在");
        }
        node.value = value;
    }

    /**
     * 返回以node为根节点的二分搜索树的最小值所在节点
     * @param node
     * @return
     */
    private Node Minimum(Node node){
        if (node.left == null){
            return node;
        }
        return Minimum(node.left);
    }

    /**
     * 删除二分搜索树中的最小值
     * @param node
     * @return
     */
    public Node removeMin(Node node){
        if (node.left == null){
            Node rightNode = node.right;
            node.right = null;
            size--;
            return rightNode;
        }

        node.left = removeMin(node.left);
        return node;
    }

    /**
     * 从二分搜树中删除键为key的节点
     * @param key
     * @return
     */
    public V remove(K key){
        Node node = getNode(root, key);
        if (node != null){
            root = remove(root, key);
            return node.value;
        }
        return null;
    }

    private Node remove(Node node, K key){
        if (node == null){
            return null;
        }

        if (key.compareTo(node.key) < 0){
            node.left = remove(node.left, key);
            return node;
        }else if (key.compareTo(node.key) > 0){
            node.right = remove(node.right, key);
            return node;
        }else { //当前节点就是要删除的节点的情况
            //待删除节点的左子树为空的情况
            if (node.left == null){
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }else if (node.right == null){
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }else{
                //待删除的节点的左右子树皆不为空
                //找到待删除节点右子树中最小的节点
                Node successor = Minimum(node.right);
                successor.right = remove(node.right, successor.key);
                successor.left = node.left = null;
                return successor;
            }
        }
    }


}
